
import { AppServiceBase, Http } from "ibiz-core";
import { SyncSeriesHook } from "qx-util";
import { LocalAuthGuard } from "./local-auth-guard";
import { RemoteAuthGuard } from "./remote-auth-guard";
import { check } from "./verify";

/**
 * @description 预览工具类
 * @export
 * @class PreviewUtil
 */
export class PreviewUtil {

  /**
   * @description 预览工具类唯一实例
   * @static
   * @type {PreviewUtil}
   * @memberof PreviewUtil
   */
  static instance: PreviewUtil;

  /**
   * @description 钩子
   * @static
   * @memberof PreviewUtil
   */
  public static hooks = {
    dcSystemBefore: new SyncSeriesHook<[], { dcsystem: string }>(),
    dcSystemAfter: new SyncSeriesHook<[], { dcsystem: string, data: any }>(),
    appBefore: new SyncSeriesHook<[], { url: string, param: any }>(),
    appAfter: new SyncSeriesHook<[], { data: any }>()
  };

  /**
   * @description 获取预览工具类实例
   * @static
   * @return {*}  {PreviewUtil}
   * @memberof PreviewUtil
   */
  static getInstance(): PreviewUtil {
    if (!this.instance) {
      this.instance = new PreviewUtil();
    }
    return this.instance;
  }

  /**
   * @description 是否启用本地服务
   * @private
   * @type {boolean}
   * @memberof PreviewUtil
   */
  private useLocalService: boolean = true;

  /**
   * @description 初始化预览
   * @param {*} router
   * @param {*} to
   * @memberof PreviewUtil
   */
  async initPreview(router: any, to: any) {
    await this.getConfig(to);
    if (this.useLocalService) {
      return await LocalAuthGuard.getInstance().authGuard(router, PreviewUtil.hooks);
    } else {
      return await RemoteAuthGuard.getInstance().authGuard(router, PreviewUtil.hooks);
    }
  }

  /**
   * @description 获取配置
   * @param {*} router
   * @param {*} to
   * @param {*} from
   * @return {*}  {Promise<void>}
   * @memberof PreviewUtil
   */
  async getConfig(to: any): Promise<void> {
    await this.initServiceConfig();
    let response: any = {};
    if (to.params['type'] && check(to.params['type'].toUpperCase(), to.params['modelPath'])) {
      response = await Http.getInstance().post('/changeModel', { type: to.params['type'].toUpperCase(), path: to.params['modelPath'] });
    } else {
      response = await Http.getInstance().post('/appConfig');
    }
    if (response && response.status == 200) {
      this.computeParams(to, response.data);
    }
  }

  /**
   * @description 初始化服务配置
   * @memberof PreviewUtil
   */
  async initServiceConfig() {
    const response: any = await Http.getInstance().post('/getServiceConfig');
    if (response && response.status == 200) {
      this.useLocalService = response.data['useLocalService'];
      AppServiceBase.getInstance().setUseLocalService(this.useLocalService);
    }
  }

  /**
   * @description 计算路由参数
   * @private
   * @param {*} to
   * @param {*} data
   * @memberof PreviewUtil
   */
  private computeParams(to: any, data: any) {
    if (data['type']) {
      to.params['type'] = data['type'];
    }
    if (data['path']) {
      to.params['modelPath'] = decodeURIComponent(data['path']);
    }
  }

}